package com.aa;




import javax.crypto.NullCipher;

import com.aa.activity.LoginActivity;
import com.aa.activity.RegisterActivity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;

public class YaloeLauncherActivity extends Activity {

	private Handler mHandler;
	private Thread mThread;
	private int mCount = 5;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.launcher);
		Common.getUserInfo(YaloeLauncherActivity.this);
		mHandler = new Handler();
		mThread = new ServiceWaitThread();
		mThread.start();
		addShortcut();
	}
	
	protected void onServiceReady()
	{
	    this.mHandler.postDelayed(new Runnable()
	    {
	      public void run()
	      {
	    	  
	    	  if (Common.iUID != null && Common.iUID.length() != 0 && Common.iPassword != null
	    			  && Common.iPassword.length() != 0 && Common.iMyPhoneNumber != null && Common.iMyPhoneNumber.length() != 0) {
	    		 Intent _Intent = new Intent();
	  	        _Intent.setClass(YaloeLauncherActivity.this, YaloeActivity.class);
	  	        startActivity(_Intent);
			}else {
				  Intent _Intent = new Intent();
			        _Intent.setClass(YaloeLauncherActivity.this, LoginActivity.class);
			        startActivity(_Intent);
			}
	    	  
	    	  
	      
	        finish();
	      }
	    }
	    , 300L);
	}
	
	 private class ServiceWaitThread extends Thread
	  {
	    private ServiceWaitThread()
	    {
	    }

	    public void run()
	    {
	      while (true)
	      {
	        if (mCount <= 0)
	        {
	         mHandler.post(new Runnable()
	          {
	            public void run()
	            {
	              onServiceReady();
	            }
	          });
	        
	        mThread = null;
	        return;
	       }
	        try
	        {
	          sleep(30L);
	         mCount--;;
	        }
	        catch (InterruptedException localInterruptedException)
	        {
	        	 throw new RuntimeException("waiting thread sleep() has been interrupted");
	        }
	      
	      }
	    }
	  }
	 private boolean hasShortcut()
	  {
	    Uri localUri = Uri.parse("content://com.android.launcher.settings/favorites?notify=true");
	    ContentResolver localContentResolver = getContentResolver();
	    String[] arrayOfString1 = { "title", "iconResource" };
	    String[] arrayOfString2 = new String[1];
	    arrayOfString2[0] = getString(R.string.app_name).trim();
	    Cursor localCursor = localContentResolver.query(localUri, arrayOfString1, "title=?", arrayOfString2, null);
	    boolean bool = false;
	    if (localCursor != null)
	    {
	      int i = localCursor.getCount();
	      if (i > 0)
	        bool = true;
	    }
	    return bool;
	  }
	 private void addShortcut()
	  {
	    if (hasShortcut())
	      return;
	    Intent localIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
	    localIntent.putExtra("android.intent.extra.shortcut.NAME", getString(R.string.app_name)); 

	    localIntent.putExtra("duplicate", false);
	    localIntent.addCategory("android.intent.category.LAUNCHER");
	    ComponentName localComponentName = new ComponentName(getPackageName(), getPackageName() + "." + getLocalClassName());
	    localIntent.putExtra("android.intent.extra.shortcut.INTENT", new Intent("android.intent.action.MAIN").setComponent(localComponentName));
	    localIntent.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher));
	    sendBroadcast(localIntent);
	  }
}
